// 3. 无重复字符的最长子串

/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
    let length = s.length;
    let result = [];
    let alternative = [];
    if (s.length === 0) {
        return 0;
    }

    for (let i = 0; i < length; i++) {
        let element = s[i];
        let elementIndex = result.indexOf(element);
        if (elementIndex === -1) {
            result.push(element);
            continue;
        }

        if (elementIndex === 0) {
            result.shift();
            result.push(element);
            continue;
        }

        if (elementIndex === result.length - 1) {
            alternative.push(result.join(""));
            result = [element];
        }

        alternative.push(result.join(""));
        result = [...result.slice(elementIndex + 1), element];
    }

    if (result.length > 0) {
        alternative.push(result.join(""));
    }

    alternative.sort((a, b) => b.length - a.length);

    console.log("momo", alternative);

    return alternative[0].length;
};

// console.log("result: 3", lengthOfLongestSubstring("abcabcbb"));
// console.log("result: 1", lengthOfLongestSubstring("bbbbb"));
// console.log("result: 3", lengthOfLongestSubstring("pwwkew"));
console.log("result: 6", lengthOfLongestSubstring("ohvhjdml"));
